package stackAndQueue;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/23 9:59
 */
public class MinStack {
    private final Deque<Integer> Stack = new LinkedList<>();
    private final Deque<Integer> minStack = new LinkedList<>();

    public MinStack() {

    }

    public void push(int val) {
        Stack.push(val);
        if (minStack.isEmpty()){
            minStack.push(val);
        }else if (val <= minStack.peek()){//小于等于都放
            minStack.push(val);
        }
    }

    public void pop() {
        if (Stack.isEmpty()) {
            return;
        }
        //change
        int val = Stack.peek();
        if(val == minStack.peek()){
            minStack.pop();
        }
        Stack.pop();
        //

    }

    public int top() {
        if (Stack.isEmpty()) {
            return -1;
        }

        return Stack.peek();      // [0] 元素就是栈顶元素
    }

    public int getMin() {
        if(minStack.isEmpty()){
            return -1;
        }
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack stack = new MinStack();

        stack.push(5);
        stack.push(3);
        stack.push(7);
        stack.push(1);
        stack.push(1);
        stack.push(0);
        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
    }
}
